<script>
  import config from './config'
  import { getToken } from '@/utils/auth'
	import {
		mapState,
		mapMutations
	} from 'vuex';
  export default {
    onLaunch: function() {
      this.initApp()
    },
	computed: mapState(['amapInfo']),
    methods: {
      // 初始化应用
      initApp() {
        // 初始化应用配置
        this.initConfig()
		// #ifdef APP-PLUS
		let that = this;
		//阻止再按一次退出应用
		that.moveBack();
		//获得保存在本地的状态信息
		uni.getStorage({
			key: 'amapInfo',
			success: res => {
				if (res.data) {
					that.setAmapInfo(res.data);
					console.log(that.amapInfo);
					if (that.amapInfo.hasOpen) {
						that.start();
					}
				}
			}
		})
		// #endif
        // 检查用户登录状态
        //#ifdef H5
        this.checkLogin()
        //#endif
      },
      initConfig() {
        this.globalData.config = config
      },
      checkLogin() {
        if (!getToken()) {
          this.$tab.reLaunch('/pages/login') 
        }
      },
	  moveBack() {
	  	//再按一次退出应用，改为退至后台
	  	let main = plus.android.runtimeMainActivity();
	  	//为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台  
	  	plus.runtime.quit = function() {
	  		main.moveTaskToBack(false);
	  	};
	  	//重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用，其他正常toast  
	  	plus.nativeUI.toast = (function(str) {
	  		if (str == '再按一次退出应用') {
	  			main.moveTaskToBack(false);
	  			return false;
	  		} else {
	  			uni.showToast({
	  				title: str,
	  				icon: 'none',
	  			})
	  		}
	  	});
	  },
	  //开启持续定位
	  start() {
	  	let that = this;
	  	that.amapInfo.lunaAmap = uni.requireNativePlugin("Luna-Amap");
	  	// 插件内置方法使用
	  	let getStart = true;
	  	that.amapInfo.lunaAmap.checkPermission({
	  		checkGPS: true, //检测GPS是否开启
	  		checkLocation: true, //检测基础定位服务是否开启
	  		checkBackLocation: true, //检测后台定位是否开启
	  		checkWakeLock: true, //检测后台唤醒应用是否开启
	  		checkBattery: true, //检测是否加入电池优化白名单
	  		// checkWIFI:true, 
	  	}, res => {
	  		console.log(res)
	  		if (!res.IgnoreBatteryOptimizationsEnabled) { //电池优化未优化
	  			//设置忽略电池优化
	  			getStart = false;
	  			that.amapInfo.lunaAmap.setBatteryOptimizations(msg => {
	  				console.log(msg)
	  			})
	  		}
	  		if (!res.wifiEnabled) { //WIFI模块未开启
	  			// getStart = false;
	  			//打开WIFI模块
	  			that.amapInfo.lunaAmap .openWIFI(msg => {
	  				console.log(msg)
	  			})
	  		}
	  		if (!res.wakeLockEnabled) { //后台唤醒应用未启动，注：应用销毁时，wakeLock锁立即释放
	  			//开启后台唤醒应用
	  			that.amapInfo.lunaAmap.openWakeLock(msg => {
	  				console.log(msg)
	  			})
	  		}
	  		if (!res.gpsEnabled || !res.locationEnabled || !res.backLocationEnabled) { //定位三件套有一个未开启时
	  			//开启定位权限组
	  			getStart = false;
	  			that.amapInfo.lunaAmap.openLocationPermissionGroup(msg => {
	  				console.log(msg);
	  			})
	  		}
	  		// if (!getStart) {
	  		// 	console.log("延迟3秒启动");
	  		// 	setTimeout(res => {
	  		// 		that.startLocation();
	  		// 	}, 3000)
	  		// } else {
	  		// 	console.log("立即启动");
	  		// 	that.startLocation();
	  		// }
	  	})
	  },
	  ...mapMutations(['setAmapInfo'])
    }
  }
</script>

<style lang="scss">
  @import '@/static/scss/index.scss'
</style>
